package com.kyzh.core.adapters

import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.gushenge.atools.ui.ArcButton
import com.gushenge.core.beans.Sort
import com.gushenge.core.impls.GameImpl
import com.gushenge.core.listeners.ResultListener
import com.kyzh.core.R
import org.jetbrains.anko.toast

class RankTopAdapter(val context: Context, val titles: ArrayList<String>) : PagerAdapter() {
    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun getCount(): Int {
        return titles.size
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = View.inflate(context, R.layout.rank_top_viewpager, null)
        val tab = view.findViewById<TabLayout>(R.id.tabLayout)
        val viewpager = view.findViewById<ViewPager>(R.id.viewpager)
        GameImpl.sort(object : ResultListener {
            override fun success(lists: Any) {

                lists as ArrayList<Sort>
                val adapter = RankBottomAdapter(context, lists, position)
                viewpager.adapter = adapter
                tab.setupWithViewPager(viewpager)
                viewpager.currentItem = 0
                for (i in 0 until lists.size) {
                    val tab1 = tab.getTabAt(i)
                    tab1?.customView = adapter.getTabView(i)
                }
                tab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                    override fun onTabReselected(tab: TabLayout.Tab?) {
                        tab?.customView?.findViewById<ArcButton>(R.id.text)?.setBackgroundColor(Color.rgb(223, 235, 255))
                        tab?.customView?.findViewById<ArcButton>(R.id.text)?.setTextColor(context.resources.getColor(R.color.colorPrimary))
                    }

                    override fun onTabSelected(tab: TabLayout.Tab?) {
                        tab?.customView?.findViewById<ArcButton>(R.id.text)?.setBackgroundColor(Color.rgb(223, 235, 255))
                        tab?.customView?.findViewById<ArcButton>(R.id.text)?.setTextColor(context.resources.getColor(R.color.colorPrimary))
                    }

                    override fun onTabUnselected(tab: TabLayout.Tab?) {
                        tab?.customView?.findViewById<ArcButton>(R.id.text)?.setBackgroundColor(Color.rgb(244, 244, 244))
                        tab?.customView?.findViewById<ArcButton>(R.id.text)?.setTextColor(context.resources.getColor(R.color.font_66))
                    }
                })

                tab.selectTab(tab.getTabAt(0))
            }

            override fun error(error: String) {
                context.toast(error)
            }
        })
        container.addView(view)
        return view
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return titles[position]
    }
}